package com.lvyou.ctrl;


import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lvyou.entity.UserModel;
import com.lvyou.model.JsonResult;
import com.lvyou.service.LoginSrv;


@RestController
public class LoginCtrl {

	@Resource(name="LoginSrv")
	private LoginSrv loginSrv;
	
	
	@PostMapping("doLogin")
	public JsonResult login(@RequestParam String phone,@RequestParam String password,HttpSession session){
		JsonResult jr=new JsonResult(0,"登录成功！");
		try {
			Map resultMap=loginSrv.doLogin(phone, password);
			int code=(Integer)resultMap.get("code");
			if(code==1){
				UserModel um=(UserModel)resultMap.get("userInfo");
				session.setAttribute("userInfo", um);
			}else{
				jr.setCode(200);
				jr.setMsg("用户不存在或密码错误！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(200);
			jr.setMsg("用户不存在或密码错误！");
		}
		return jr;
	}

	@RequestMapping("/toLogin")
	public JsonResult toLogin(){
		JsonResult jr=new JsonResult(300,"请登录");
		return jr;
	}
}
